package com.sensorfriendly.ui;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sensorfriendly.AppConstants;
import com.sensorfriendly.AppContext;
import com.sensorfriendly.R;
import com.sensorfriendly.model.Message;
import com.sensorfriendly.util.CallUtil;

/**
 * 
 * Showing a list of messages that use can customize.
 *
 */
public class EditMessage extends ListActivity implements OnClickListener{

	private List<Message> messages;
	private String category;
	private MessageListAdapter adapter;
	private ListView lv ;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_message);
        
        ((TextView) findViewById(R.id.page_title)).setText("Edit Messages");
		((ImageView) findViewById(R.id.ab_settings)).setOnClickListener(this);
		((ImageView) findViewById(R.id.ab_home)).setOnClickListener(this);
		((ImageView) findViewById(R.id.ab_call)).setVisibility(View.GONE);
        
        
		final AppContext app = (AppContext) getApplication();
		
		Bundle extra = this.getIntent().getExtras();
		category = extra.getString(AppConstants.CATEGORY);
		
        setTitle(category.toUpperCase());
        
		messages = app.getScenarioMessages(category);
        
		if(messages!=null)
		{
			// Create the adapter
			adapter = new MessageListAdapter(this, category, messages);
			
			// Selecting single ListView item
			lv = getListView();

			lv.setAdapter(adapter);
		}
		
		// Get the button to add to the List of Scenarios
		Button btn = (Button)findViewById(R.id.btnAdd);
			
		// Add the new Category
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText newMessages = (EditText) findViewById(R.id.txtItem);
				
				// If there is no text, prompt the user
				if(newMessages == null || newMessages.getText().toString().trim().matches(""))
				{
					Toast.makeText(EditMessage.this, "Please enter message and then press 'Add'", Toast.LENGTH_SHORT).show();
					return;
				}
				Message msg = new Message();
				msg.setMessageContent(newMessages.getText().toString());
				msg.setScenarioName(category);
				app.addNewMessage(msg);
				newMessages.setText("");
				
				if(messages == null){ 
					messages = app.getScenarioMessages(category);
					adapter = new MessageListAdapter(EditMessage.this, category, messages);
					lv = getListView();
					lv.setAdapter(adapter);
				}
				else{
					messages = app.getScenarioMessages(category);
					adapter.setMessages(messages);
					adapter.notifyDataSetChanged();
					lv.setAdapter(adapter);
				}
			}
		});
    }
    
    
    @Override
    public void onResume(){
    	super.onResume();
		AppContext app = (AppContext) getApplication();
		messages = app.getScenarioMessages(category);
		adapter.setMessages(messages);
    	adapter.notifyDataSetChanged();
    	lv.setAdapter(adapter);
    }
    
    
    /**
     * Handling click of buttons on the action bar.
     */
	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.ab_settings) {
			Intent settingsIntent = new Intent(this, SettingsActivity.class);
			startActivity(settingsIntent);
		} else if (v.getId() == R.id.ab_home) {
			Intent homeIntent = new Intent(this, DashboardNavigationActivity.class);
			homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(homeIntent);
		} 
	}
    
    public void onPause()
    { 	
    	super.onPause();
    }

}
